//
//  CustomerTimer.swift
//  Motion
//
//  Created by 悦月越悦 on 2021/4/7.
//

import SwiftUI

enum TimerType {
    case Add
    case Subtract
}

class CustomerTimer:ObservableObject{
    @Published var timer : Timer?
    @Published var count :Int
    @Published var interval:Double
    @Published var type: TimerType

    init(interval:Double,count:Int,type:TimerType) {
        self.count = count
        self.type = type
        self.interval = interval
    }
    
    func startTimer(){
        timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true){ _ in
            if(self.type == .Add){
                self.count += 1
            }
            if(self.type == .Subtract){
                self.count -= 1
                if(self.count == 0){
                    self.cancel()
                }
            }
        }
    }
    
    func cancel(){
        timer?.invalidate()
        timer = nil
        count = 0 
    }
}
